<!--需要复杂的请参考:https://preview.pro.loacg.com/dashboard/analysis-->
<template>
  <a-card :bordered="false" title="我的已审">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="Id">申请单号</a-select-option>
                <a-select-option key="Remark">服务清单</a-select-option>
                <a-select-option key="StateName">状态</a-select-option>
                <a-select-option key="CreatorName">申请人</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="onApprovalSearch">查询</a-button>
            <!-- <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button> -->
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="approvalData"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleShow(record)" v-if="record.State == 0">编辑</a>
          <a @click="handleShow(record)" v-if="record.State >= 1">查看</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <approval ref="approval" :parentObj="this"></approval>
    <request-user ref="requestUser" :parentObj="this"></request-user>
  </a-card>
</template>

<script>
import ChartCard from '@/components/ChartCard/ChartCard'
import Trend from '@/components/ChartCard/Trend'
import EditForm from '@/views/GBT_Manage/GBT_ServiceRequest/EditForm'
import Approval from '@/views/GBT_Manage/GBT_ServiceRequest/Approval'
import RequestUser from '@/views/GBT_Manage/GBT_ServiceRequest/RequestUser'

const columns = [
  { title: '申请单号', dataIndex: 'Id', width: '15%' },
  { title: '服务清单', dataIndex: 'Remark', width: '15%' },
  { title: '审批人', dataIndex: 'ApprovalName', width: '10%' },
  { title: '状态', dataIndex: 'StateName', width: '10%' },
  { title: '申请人', dataIndex: 'CreatorName', width: '10%' },
  { title: '申请时间', dataIndex: 'CreateTime', width: '20%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } },
]

var userId = ''
var isApproval = true
var userInfo = {}

export default {
  components: {
    ChartCard,
    Trend,
    EditForm,
    Approval,
    RequestUser,
  },
  mounted() {
    userInfo = JSON.parse(localStorage.getItem('userInfo'))
    userId = userInfo.Id
    this.pagination.current = 1
    this.queryParam.State = 4
    this.queryParam.UserId = userId
    this.queryParam.Approver = null
    isApproval = true
    this.getDataList()
  },
  data() {
    return {
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: [],
      data: [],
      approvalData: [],
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList() {
      this.loading = true
      this.queryParam.PT = "A"
      console.log(this.queryParam)
      this.$http
        .post('/GBT_Manage/GBT_ServiceRequest/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          this.loading = false
          if (resJson.Success) {
            let list = resJson.Data
            list.forEach((element) => {
              switch (element.State) {
                case 0:
                  element.StateName = '发起'
                  break
                case 1:
                  element.StateName = '审批'
                  break
                case 2:
                  element.StateName = '确认完工'
                  break
                case 3:
                  element.StateName = '评价'
                  break
                case 4:
                  element.StateName = '完结'
                  break
              }
            })
            if (isApproval == true) {
              this.approvalData = list
            } else {
              this.data = list
            }
            console.log(list)
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onApprovalSearch() {
      userId = userInfo.Id
      this.pagination.current = 1
      this.queryParam.State = 4
      this.queryParam.UserId = userId
      this.queryParam.Approver = null
      isApproval = true
      this.getDataList()
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleShow(item) {
      let param = { item: item, enable: false }
      this.$refs.editForm.openForm(param)
    },
    approal(param) {
      var thisObj = this
      thisObj.loading = true
      this.$http.post('/GBT_Manage/GBT_ServiceRequest/Approval', param).then((resJson) => {
        thisObj.loading = false
        if (resJson.Success) {
          thisObj.$message.success('操作成功!')
        } else {
          thisObj.$message.error(resJson.Msg)
        }
        thisObj.getDataList()
      })
    },
    handleSubmit(item) {
      this.$refs.approval.openForm(item)
    },
    handleEvaluate(item) {
      this.$refs.requestUser.openForm(item.Id)
    },
  },
}
</script>